package cn.com.winning.ecare.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.winning.ecare.activity.CheckUpActivity.MyMainAdapter;
import cn.com.winning.ecare.common.MyApplication;
import cn.com.winning.ecare.expandable.ActionSlideExpandableListView;
import cn.com.winning.ecare.utils.DisplayUtil;
import cn.com.winning.ecare.utils.HTTPGetTool;
import cn.com.winning.ecare.utils.URLUtils;
import cn.com.winning.ecare.widgets.CustomProgressDialog;
import cn.com.winning.ecare.widgets.InnerListView;

/**临床路径-标准住院流程（临床路径表单）列表内容
 * @author Administrator
 *
 */
public class ClinicPathFour extends Activity
{

	private Intent mIntent = null;

	private ActionSlideExpandableListView list_main;
	private CustomProgressDialog proDialog;// 加载框
	private ClinicPathFour oThis = this;
	private List<Map<String, Object>> listmain = new ArrayList<Map<String, Object>>();
	private List<Map<String, Object>> listsub = new ArrayList<Map<String, Object>>();
	MyMainAdapter mainAdapter = null;
	MySubAdapter subAdapter = null;
	private String str4_id;
	MyApplication myApplication;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.clinicpath_four);
		myApplication = (MyApplication) getApplication();
		myApplication.addActivity(this);

		// 确定view
		findView();

		// 初始化界面数据
		GetData();

	}

	private void findView()
	{
		list_main = (ActionSlideExpandableListView) findViewById(R.id.list);

		mIntent = getIntent();

		if (mIntent != null)
		{
			str4_id = mIntent.getStringExtra("str4_id");
		}

	}

	private void GetData()
	{
		proDialog = CustomProgressDialog.createDialog(oThis);
		proDialog.setMessage("数据加载中...");
		proDialog.show();
		proDialog.setCanceledOnTouchOutside(false);
		// 这里单开一个线程和后台数据交互
		new Thread(new LoadData()).start();

	}

	class LoadData implements Runnable
	{

		public void run()
		{
			
			Looper.prepare();
			iniData();
			Looper.loop();
		}
	}

	private void iniData()
	{
		try
		{
			Message msg = myHandler.obtainMessage();
			List<NameValuePair> ls = new ArrayList<NameValuePair>();
			// ls.add(new BasicNameValuePair("userid", myApp.getAccount()));
			ls.add(new BasicNameValuePair("id", str4_id));
			JSONObject json = HTTPGetTool.getTool().post(URLUtils.HOST + URLUtils.CLINICPATH2, ls);
			JSONArray jsonArr;
			List<Map<String, Object>> tempmain = new ArrayList<Map<String, Object>>();
			List<Map<String, Object>> tempchildren = new ArrayList<Map<String, Object>>();
			Map<String, Object> tempmap = new HashMap<String, Object>();
			if (json != null)
			{
				if (!json.toString().equals("{\"msg\":\"系统错误！\",\"success\":\"false\"}"))
				{
					if (new JSONArray(json.getString("jsonstr")).length() > 0)
					{
						jsonArr = new JSONArray(json.getString("jsonstr"));
						List<Map<String, Object>> list;
						for (int i = 0; i < jsonArr.length(); i++)
						{
							JSONObject obj_main = jsonArr.getJSONObject(i);
							Map<String, Object> map_main = new HashMap<String, Object>();
							Map<String, Object> map_sub = new HashMap<String, Object>();
							map_main.put("sxmc", obj_main.getString("sxmc"));
							map_sub.put("sxnr", obj_main.getString("sxnr"));
							tempmain.add(map_main);
							tempchildren.add(map_sub);
						}
						msg.what = 0;
						tempmap.put("tempmain", tempmain);
						tempmap.put("tempchildren", tempchildren);
						msg.obj = tempmap;
					}
					else
					{
						msg.what = -1;
						msg.obj = "暂无数据";
					}

				}
				else
				{
					msg.what = -1;
					msg.obj = "系统错误";
				}
			}
			else
			{
				msg.what = -1;
				msg.obj = "网络连接失败!";
			}
			myHandler.sendMessage(msg);
		}
		catch (Exception e)
		{
			
			e.printStackTrace();
		}
	}

	/**
	 * 主线程状态更新
	 */
	Handler myHandler = new Handler()
	{
		public void handleMessage(Message msg)
		{
			switch (msg.what)
			{
				case -1:
					proDialog.dismiss();
					Toast.makeText(oThis, (String)msg.obj, Toast.LENGTH_SHORT).show();
					break;
				case 0:
					if (proDialog != null && proDialog.isShowing())
					{
						proDialog.dismiss();
					}
					Map<String, Object> map = (Map<String, Object>) msg.obj;
					listmain = (List<Map<String, Object>>) map.get("tempmain");
					listsub = (List<Map<String, Object>>) map.get("tempchildren");
					if (mainAdapter == null)
					{
						mainAdapter = new MyMainAdapter();
					}
					list_main.setAdapter(mainAdapter);
					mainAdapter.notifyDataSetChanged();
					break;

				default:
					break;
			}
		}
	};

	/**
	 * 自定义listview布局显示内容
	 */
	class MyMainAdapter extends BaseAdapter
	{

		public int getCount()
		{
			
			return listmain.size();
		}

		public Object getItem(int position)
		{
			
			return listmain.get(position);
		}

		public long getItemId(int position)
		{
			
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			
			View view = convertView;
			Map<String, Object> map_main = (Map<String, Object>) getItem(position);
			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.clinicpath_four_main, null);
			TextView tv4_mc = (TextView) view.findViewById(R.id.tv4_mc);
			tv4_mc.setText(map_main.get("sxmc").toString());
			InnerListView list_sub = (InnerListView) view.findViewById(R.id.list_clinicpath_sub);
			ScrollView scroll_view = (ScrollView) view.findViewById(R.id.scroll_view);
			Map<String, Object> map_sub = (Map<String, Object>) listsub.get(position);
			subAdapter = new MySubAdapter(oThis, map_sub);
			list_sub.setAdapter(subAdapter);
			list_sub.setParentScrollView(scroll_view);
			list_sub.setMaxHeight(DisplayUtil.dip2px(oThis, 300));
			return view;
		}
	}

	class MySubAdapter extends BaseAdapter
	{

		Context context;
		Map<String, Object> children;

		public MySubAdapter(Context context, Map<String, Object> children)
		{
			super();
			this.context = context;
			this.children = children;
		}

		public int getCount()
		{
			
			return 1;
		}

		public Object getItem(int position)
		{
			
			return children;
		}

		public long getItemId(int position)
		{
			
			return 1;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			
			View view = convertView;

			view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.clinicpath_four_sub, null);
			TextView item = (TextView) view.findViewById(R.id.item);
			item.setText(children.get("sxnr").toString());
			return view;
		}

	}

}
